package com.olinsports.system.olinsportssystem.service;

import com.olinsports.system.olinsportssystem.model.MemberInformation;
import com.olinsports.system.olinsportssystem.model.SavingDetail;
import com.olinsports.system.olinsportssystem.model.SiteConsumptionDetails;
import com.olinsports.system.olinsportssystem.repository.SiteConsumptionDetailsRepository;
import com.olinsports.system.olinsportssystem.util.PageUtil;
import com.olinsports.system.olinsportssystem.util.SavingDetailExcel;
import com.olinsports.system.olinsportssystem.util.SiteConsumptionDetailsExcel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/22.
 */
@Service
public class SiteConsumptionDetailsService {
    @Autowired
    private SiteConsumptionDetailsRepository siteConsumptionDetailsRepository;
   
    private int totalPages = 0;//总页数

    private int  totalRows;//总数据量
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;


    public SiteConsumptionDetails selectByKey(int siteDeatilsKey){
        return siteConsumptionDetailsRepository.selectByKey(siteDeatilsKey);
    }

    public Map listSiteDetails(String page, String rows,String sord, String sidx){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
            //查询当前表中的总数据量
            totalRows = siteConsumptionDetailsRepository.findCount();
            System.out.println("totalRows = "+totalRows);
            //通过分页进行查询当前表中的数据
            List<SiteConsumptionDetails> siteConsumptionDetailss =  siteConsumptionDetailsRepository.findLimit(sord,sidx,beg,Integer.valueOf(rows));

            Map mapList = pageUtil.paging(rows,page,totalRows);
            mapList.put("rows",siteConsumptionDetailss);//设置行显示的数据

            return mapList;
    }

    public Map searchDate(String page, String rows,String startDate,String endDate){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = siteConsumptionDetailsRepository.selectByDateCount(startDate,endDate);
        List<SiteConsumptionDetails> siteConsumptionDetailss =  siteConsumptionDetailsRepository.selectByDateLimit(startDate,endDate,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",siteConsumptionDetailss);//设置行显示的数据
        return mapList;
    }


    public ModelAndView toDcExcel(String startDate, String endDate, HSSFWorkbook workbook) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        List<SiteConsumptionDetails> siteConsumptionDetailss = siteConsumptionDetailsRepository.selectByDateAll(startDate,endDate);
        map.put("inventoryDetailss",siteConsumptionDetailss);
        SiteConsumptionDetailsExcel siteConsumptionDetailsExcel = new SiteConsumptionDetailsExcel("场地消费明细","场地消费明细");
        siteConsumptionDetailsExcel.buildExcelDocument(map, workbook, request, response);
        return new ModelAndView(siteConsumptionDetailsExcel, map);
    }

    public Map searchSelf(String page, String rows,String field,String fieldValue){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = siteConsumptionDetailsRepository.selectSelfCount(field,fieldValue);
        List<SiteConsumptionDetails> siteConsumptionDetailss =  siteConsumptionDetailsRepository.selectSelfLimit(field,fieldValue,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",siteConsumptionDetailss);//设置行显示的数据
        return mapList;
    }

    public Map searchMoreSelf(String page, String rows,String fieldValue){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = siteConsumptionDetailsRepository.selectSelfMoreCount(fieldValue);
        List<SiteConsumptionDetails> siteConsumptionDetailss =  siteConsumptionDetailsRepository.selectSelfMoreLimit(fieldValue,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",siteConsumptionDetailss);//设置行显示的数据
        return mapList;
    }

    public int getSumTotal(){
        List<SiteConsumptionDetails> siteConsumptionDetailss = siteConsumptionDetailsRepository.selectAll();
        int sumTotal = 0;
        for(SiteConsumptionDetails siteConsumptionDetails : siteConsumptionDetailss){
            sumTotal += siteConsumptionDetails.getPrice();
        }
        return sumTotal;
    }
}
